package main

import "fmt"

func Sum(S []int, ch chan int) {
	sum := 0
	//计算数组和
	for _, v := range S {
		sum += v
	}
	//放入通道
	ch <- sum
}

func main() {
	s := []int{6, 7, 8, -9, 1, 8}
	ch := make(chan int)
	//用一个goroutine计算前半个数组的和
	go Sum(s[:len(s)/2], ch)
	//用第二个goroutine计算前后个数组的和
	go Sum(s[len(s)/2:], ch)
	//从通道取出两个和
	a, b := <-ch, <-ch
	fmt.Println(a, b, a+b)
}
